﻿using Prism.Commands;
using Prism.Events;
using Prism.Mvvm;
using Prism.Regions;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using WpfApp_SmartParking_Base;
using WpfApp_SmartParking_Entities;
using WpfApp_SmartParking_IService;
using WpfApp_SmartParking_Models;

namespace WpfApp_SmartParking.ViewModels.Pages;

public class MenuManagementViewModel:ViewModelBase
{
    private ObservableCollection<MenuItemModel> _menus = new ObservableCollection<MenuItemModel>();

    public ObservableCollection<MenuItemModel> Menus
    {
        get { return _menus; }
        set { SetProperty<ObservableCollection<MenuItemModel>>(ref _menus, value); }
    }

    private readonly IMenuService _menuService;

    private readonly IDialogService _dialogService;

    private readonly IEventAggregator _eventAggregator;

    private List<SysMenu>? sysMenus = null;
   

    public MenuManagementViewModel(
        IRegionManager regionManager, 
        IMenuService menuService,
        IDialogService dialogService,
        IEventAggregator eventAggregator) :base(regionManager)
    {
        PageTitle = "菜单管理";
        IsCanClose = true;

        _menuService = menuService;
        _dialogService = dialogService;
        _eventAggregator = eventAggregator;

        RefreshMenuCommand = new DelegateCommand<string>(DoSearchMenu);
        OpenMenuDetailWindowCommand = new DelegateCommand<Object?>(DoOpenMenuDetailWindow);
        DeleteMenuCommand = new DelegateCommand<Object?>(DoDeleteMenu);

        InitMenus();
    }

    private void InitMenus()
    {
        sysMenus = _menuService.GetMenuList().ToList();
        Menus = LoadMenus(0);
    }

    /// <summary>
    /// 加载菜单
    /// </summary>
    private ObservableCollection<MenuItemModel> LoadMenus(int _parentId)
    {
        ObservableCollection<MenuItemModel> menuItemModels = new ObservableCollection<MenuItemModel>();
        var _items = sysMenus!.Where(it => it.ParentId == _parentId).OrderBy(it => it.Index).ToList();
        foreach (var item in _items)
        {
            MenuItemModel menuItemModel = new MenuItemModel()
            {
                MenuId = item.MenuId,
                MenuHeader = item.MenuHeader ?? "",
                MenuIcon = item.MenuIcon ?? "",
                TargetView = item.TargetView ?? "",
                IsExpanded = true,
                ParentId = item.ParentId,
                MenuType = item.MenuType,
            };
            menuItemModel.Children = LoadMenus(item.MenuId);
            if (menuItemModel.Children.Count > 0) 
            {
                menuItemModel.Children[menuItemModel.Children.Count - 1].IsLastChild = true;
            }
            menuItemModels.Add(menuItemModel);
        }
        return menuItemModels;
    }

    string _searchKey = string.Empty;

    /// <summary>
    /// 关键字搜索
    /// </summary>
    /// <param name="_key"></param>
    private void DoSearchMenu(string _key = "")
    {
        _searchKey = _key;
        sysMenus = _menuService.GetMenuList(_key).ToList();
        Menus = LoadMenus(0);
    }

    /// <summary>
    /// 打开菜单详情窗口
    /// </summary>
    /// <param name="menuItemModel"></param>
    private void DoOpenMenuDetailWindow(Object? menuItemModel)
    { 
        IDialogParameters dialogParameters = new DialogParameters();
        dialogParameters.Add("MenuModel", menuItemModel);
        _dialogService.ShowDialog("MenuDetailView", dialogParameters, result => 
        {
            if (result.Result == ButtonResult.OK)
            {
                DoSearchMenu(_searchKey);
            }
        });
    }

    /// <summary>
    /// 删除
    /// </summary>
    /// <param name="menuItemModel"></param>
    private void DoDeleteMenu(Object? menuItemModel)
    {
        MessageBoxResult messageBoxResult = MessageBox.Show("是否删除此菜单？请注意此操作不可逆？", "系统消息", MessageBoxButton.OKCancel, MessageBoxImage.Warning);
        if (messageBoxResult == MessageBoxResult.OK)
        {
            _menuService.Delete<SysMenu>((menuItemModel as MenuItemModel)!.MenuId);
            DoSearchMenu(_searchKey);
            _eventAggregator.GetEvent<RefreshMenuEvent>().Publish();
        }
    }
}
